﻿using SharpSoft.WPF;
using SharpSoft.WPF.WriteableBitmapModules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF.DEMO.Pages
{
    /// <summary>
    /// AniModulePage.xaml 的交互逻辑
    /// </summary>
    public partial class AniModulePage : Page
    {
        public AniModulePage()
        {
            InitializeComponent();
            imgani.Source = bmp;
            this.Loaded += AniModulePage_Loaded;
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += Timer_Tick;
            timer.Start();
            forceWorld.RenderEnd += ForceWorld_RenderEnd; ;
            forceWorld.Initialized += ForceWorld_Initialized;

        }

        private void ForceWorld_RenderEnd(object sender, RenderEndEventArgs e)
        {
            txt_fps.Text = $"Fps:{forceWorld.Fps} ";
            if (e.Time.TotalTime.TotalMilliseconds > 1500)
            {
                forceWorld.Field.Forces[0].IsEnabled = false;
                forceWorld.Field.Forces[1].IsEnabled = true;
            }
        }

        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();

        private void Timer_Tick(object sender, EventArgs e)
        {
            //  forceWorld.Field.Forces[0].IsEnabled ^= true;
        }

        WriteableBitmap bmp = new WriteableBitmap(1920, 1080, 96, 96, PixelFormats.Bgra32, BitmapPalettes.WebPalette);
        BoomAni boomAni = new BoomAni();
        ForceWorld forceWorld = new ForceWorld();
        private void AniModulePage_Loaded(object sender, RoutedEventArgs e)
        {
            bmp = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Bgra32, BitmapPalettes.WebPalette);
            imgani.Source = bmp;

            //boomAni.BoomCenter = new Point(0.5, 0.5);
            //boomAni.BoomRadius = 800;
            //boomAni.Acceleration = 12500;
            //boomAni.Deceleration = 300;
            //boomAni.MaxVelocity = 114500;
            //boomAni.MinVelocity = 800;
            //boomAni.VelocityRandomSeed = 300;
            //boomAni.RenderTarget = bmp;
            //boomAni.Source = imgsou.Capture();
            //boomAni.InitialPosition = new PointInt32(500, 400);
            //boomAni.TargetPosition = new PointInt32(100, 140);
            //boomAni.GridInfo = new GridInfo(40, (int)(40 * (boomAni.Source.PixelHeight / (double)boomAni.Source.PixelWidth))); //new GridInfo(boomAni.Source.PixelWidth / 2, boomAni.Source.PixelHeight / 2);
            //boomAni.Initialize();
            //boomAni.Start();
            forceWorld.GridInfo = new GridInfo(10);
            forceWorld.RenderTarget = bmp;
            forceWorld.Source = imgsou.Capture();
            forceWorld.InitialPosition = new Point(200, 400);
            forceWorld.Initialize();

            forceWorld.AddForce(new Point(forceWorld.InitialPosition.X + forceWorld.Source.PixelWidth / 2, forceWorld.InitialPosition.Y + forceWorld.Source.PixelHeight / 2), 800, 0);
            var lf = forceWorld.AddForce(500, forceWorld.InitialPosition + new Vector(400, 100));
            lf.DecayEffect = (f, o) =>
            {
                var dir = f[o] - o.Position;
                if (dir.Length < 500)
                {
                    return f.Value * 500 / dir.Length;
                }
                return f.Value;
            };
            lf.IsEnabled = false;
            //forceWorld.AddForce(new Rect(forceWorld.InitialPosition.X + 400, forceWorld.InitialPosition.Y + 100, forceWorld.Source.PixelWidth, forceWorld.Source.PixelHeight), -40);
            forceWorld.Start();

        }
        private void ForceWorld_Initialized(object sender, EventArgs e)
        {
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            forceWorld.Stop();
            forceWorld.Reset();
            forceWorld.Field.Forces[0].IsEnabled = true;
            forceWorld.Field.Forces[1].IsEnabled = false;
            forceWorld.Start();
        }
    }
}
